<%@page import="dao.Course"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
%>
<%
	Course course = (Course)session.getAttribute("course");
	int chapterCounts = course.getChaptercounts();	
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>添加题目</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<link rel="stylesheet" type="text/css"
	href="./clEditer/jquery.cleditor.css" />
<style type="text/css">
body {
	font: normal 16px auto "Trebuchet MS", Verdana, Arial, Helvetica,
		sans-serif;
	color: #4f6b72;
	text-align: left;
	background: #E6EAE9;
}

#container {
	margin-left: auto;
	margin-right: auto;
	width: 800px;
	height: 700px; 
	background: #f8f8f8;
	display: block;
	vertical-align: middle;
}

.ele {
	margin-top: 20px;
	margin-botton: 10px;
	text-align: center;
	width: 100%;
	display: block;
	vertical-align: middle;
}

lable {
	text-align: left;
}
</style>


<script type="text/javascript" src="./clEditer/jquery.min.js"></script>
<script type="text/javascript" src="./clEditer/jquery.cleditor.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $("#analysization").cleditor();
        $("#question").cleditor();
        $("#answer").cleditor();
        var opt = document.getElementById("itemType");
        selectType("1");
        addChapter();
      });
    function checkData(){
    	var source = document.getElementById("source").value.trim();
    	var chapterid = document.getElementById("chapter1").value.trim();
    	var itemType = document.getElementById("itemType").value.trim();
    	var questionfile = document.getElementById("questionfile").value;
    	
    	if(source =="-1" || chapterid =="choose"){
    			return false;					
    	}else{
    		if(itemType == "1"){    			  			
    			if(questionfile ==null || questionfile == ""){
    				var item1 = document.getElementById("item1").value;
    		    	var a = document.getElementById("a").value;
    		    	var b = document.getElementById("b").value;
    		    	var c = document.getElementById("c").value;
    		    	var d = document.getElementById("d").value;
    		    	if(item1 ==""||a =="" ||b =="" ||c =="" ||d ==""){
    		    		alert("请将问题填写完整");
    		    		return false;
    		    	}
    			}
    			var answer1 = addItemForm.answerid1;
    			var ok = false; 
    			for(var i=0;i<answer1.length;i++){
    				if(answer1[i].selected == true){
    					ok = true;
    					break;
    				}
    			}
    			if(!ok){
    				alert("请选择答案");
    				return false;
    			}
    		}else{
    			var answerfile = document.getElementById("answerfile").value;
    			if(questionfile ==null || questionfile == ""){
    				var question2 = document.getElementById("question").value.trim();
    				if(question2 ==""){
    					alert("请将问题填写完整");
    					return false;
    				}
    			}
    			if(answerfile ==null || answerfile == ""){
    				var answer2 = document.getElementById("question").value.trim();
    				if(answer2 ==""){
    					alert("请将答案填写完整");
    					return false;
    				}
    			}
    		}
    	}
    	return true;
    }
    function selectType(type){
    	var question1 = document.getElementById("questionid1");
    	var question2 = document.getElementById("questionid2");
    	var answer1 = document.getElementById("answerid1");
    	var answer2 = document.getElementById("answerid2");
    	var tips = document.getElementById("tips");
    	if(type == "1"){
    		if(question1.style.display != "block" && answer1.style.display != "block"){
    			question1.style.display = "block";
    			answer1.style.display = "block";
    		}
    		if(question2.style.display != "none" && answer2.style.display != "none"){
    			question2.style.display = "none";
    			answer2.style.display = "none";
    		} 
    	}else{
    		if(question2.style.display != "block" && answer2.style.display != "block"){
    			question2.style.display = "block";
    			answer2.style.display = "block";
    		}
    		if(question1.style.display != "none" && answer1.style.display != "none"){
    			question1.style.display = "none";
    			answer1.style.display = "none";
    		}
    		
    	}
    	if(type == "2"){
    		if(tips.style.display != "inline"){
    			tips.style.display = "inline";
    		}
    	}else{
    		if(tips.style.display != "none"){
    			tips.style.display = "none";
    		}
    	}
    }
    function addChapter(){
    	var obj = document.getElementById("chapter1");
    	var counts = <%=chapterCounts %>;
    	var courseid = "<%=course.getCourseid() %>";
    	for(var i=1;i<=counts;i++){
    		var str;
    		var value
    		if(i<10){
    			value = courseid + "0" +i;
    		}else{
    			value = courseid + i;
    		}
    		obj.options[i] = new Option("第"+i+"章",value);
    	}
    }
    function getSourceKnowledges(chapterid){
    	$.ajax({
            type:"POST",
            url:"./servlet/KnowledgeManageServlet?action=getSourceKnowledges",
            data:{chapterid:chapterid},
            beforeSend:function(){},             
            success:function(data){
            	var obj = document.getElementById("source");
            	var knowledges = data;
            	var strs = knowledges.split(';');
            	obj.length = 1;
        		for(var i=0;i<strs.length-1;i++){
        			var str1 = strs[i].split(',');
        			var value = str1[0];
        			var string = str1[1];						
        			obj.options[i+1] = new Option(string,value);
        		}
            }            
         });
    }
</script>
</head>

<body>
	<form name="addItemForm" method="post"  enctype="multipart/form-data"
		action="./servlet/ExaminationManagerServlet?action=add"
		onsubmit="return checkData()">
		<div id="container">
			<div class="ele">
				<label>所属章节：</label> <select name="chapter1" id="chapter1"
					onchange="getSourceKnowledges(this.value)">
					<option value="choose" selected="selected">请选择章节</option>
				</select> &nbsp;&nbsp;&nbsp;&nbsp; 
				<label>所属知识点</label> <select name="source" id="source">
					<option value="-1" selected="selected">请选择知识点</option>
				</select>
			</div>
			<div class="ele">
				<label>试题类型：</label> <select name="itemType" id="itemType" onchange="selectType(this.value)">
					<option value="1" selected="selected">选择题</option>
					<option value="2" >填空题</option>
					<option value="3" >解答题</option>
				</select>
			</div>
			
			<div class="ele">
			<div>
				<label>图片问题：</label><input type="file" id="questionfile" name="questionfile"> 								
			</div>
				<div id="questionid1">
					<label>问题：</label> <input type="text" name="item1" id="item1"><br>
					<label>选项A:</label><input type="text" name="a" id="a"><br>
					<label>选项B:</label><input type="text" name="b" id="b"><br>
					<label>选项C:</label><input type="text" name="c" id="c"><br>
					<label>选项D:</label><input type="text" name="d" id="d"><br>
				</div>							
			<div id="questionid2">
				<label>问题:</label><span id="tips">请在要添加填空的地方插入"&amp;nbsp"</span><br>
				<textarea id="question" name="question"></textarea>
			</div>
			</div>
			<div class="ele">
				<div id="answerid1">
					<label>答案：</label> 
					<input type="checkbox" name="choieces" value="a"><label>A</label>
					<input type="checkbox" name="choieces" value="b"><label>B</label>
					<input type="checkbox" name="choieces" value="c"><label>C</label>
					<input type="checkbox" name="choieces" value="d"><label>D</label>
				</div>
				<div id="answerid2">
				<label>图片答案：</label>
				<input type="file" id="answerfile" name="anserfile"><br>
				<label>文本答案:</label><br>
				<textarea id="answer" name="answer"></textarea>
				</div>
			</div>
			<div class="ele">
				<label>图片答案分析：</label><input type="file" id="analysizefile" name="analysizefile"><br>
				<label>文本答案分析:</label><br>				
				<textarea id="analysization" name="analysization"></textarea>
			</div>

			<div class="ele">
				<input type="submit" value="提交">&nbsp;&nbsp;&nbsp;&nbsp; 
				<input type="reset" value="取消">
			</div>
		</div>
	</form>
	<%
	    if (request.getAttribute("success") != null) {
				String str = (String) request.getAttribute("success");
				if ("true".equals(str)) {
				    out.write("<script>alert('添加成功！')</script>");
				} else if ("false".equals(str)) {
				    out.write("<script>alert('添加失败！')</script>");
				}
	    }
		if(request.getAttribute("error") != null){
		    out.write("<script>alert('请填写完整')</script>");
		}
	%>
</body>
</html>
